package io.framework.modules.hpinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.framework.common.constant.Constant;
import io.framework.common.page.PageData;
import io.framework.common.service.impl.CrudServiceImpl;
import io.framework.common.utils.ConvertUtils;
import io.framework.modules.hpinfo.dao.VTHpglHpspwrwpflDao;
import io.framework.modules.hpinfo.dao.VTHpglProjectinfoDao;
import io.framework.modules.hpinfo.dto.VTHpglHpspwrwpflDTO;
import io.framework.modules.hpinfo.entity.VTHpglHpspwrwpflEntity;
import io.framework.modules.hpinfo.entity.VTHpglProjectinfoEntity;
import io.framework.modules.hpinfo.service.VTHpglHpspwrwpflService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 环境影响评价管理-环评审批污染物排放量信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-19
 */
@Service
public class VTHpglHpspwrwpflServiceImpl extends CrudServiceImpl<VTHpglHpspwrwpflDao, VTHpglHpspwrwpflEntity, VTHpglHpspwrwpflDTO> implements VTHpglHpspwrwpflService {

    @Autowired
    private VTHpglHpspwrwpflDao hpspwrwpflDao;

    @Autowired
    private VTHpglProjectinfoDao projectinfoDao;

    @Override
    public QueryWrapper<VTHpglHpspwrwpflEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<VTHpglHpspwrwpflEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public int bindProjectInfo(Map<String, Object> params) {

        return baseDao.bindProjectInfo(params);
    }

    @Override
    public List<Map<String, Object>> getAllInfoToSelect(Map<String, Object> params) {

        return baseDao.getAllInfoToSelect(params);
    }

    @Override
    public PageData<VTHpglHpspwrwpflDTO> pages(Map<String, Object> params) {

        IPage<VTHpglHpspwrwpflEntity> pages = getPage(params, Constant.CREATE_TIME, false);

        List<VTHpglHpspwrwpflEntity> list = baseDao.getList(params);

        return getPageData(list,pages.getTotal(),VTHpglHpspwrwpflDTO.class);
    }

    @Override
    public VTHpglHpspwrwpflDTO getProjectInfoByProjectId(String id){

        //找建设单位
       VTHpglProjectinfoEntity projectEntity = projectinfoDao.selectById(id);

        List<VTHpglHpspwrwpflEntity> hpspwrwpflEntitys =  hpspwrwpflDao.getProjectInfoByProjectId(id,projectEntity.getProjectXmmc());
        VTHpglHpspwrwpflEntity entity = new VTHpglHpspwrwpflEntity();
        if(!CollectionUtils.isEmpty(hpspwrwpflEntitys)){
            entity = hpspwrwpflEntitys.get(0);
        }
        return ConvertUtils.sourceToTarget( entity, VTHpglHpspwrwpflDTO.class);
    }

}